from . import models
from rest_framework import serializers


class GoodsDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.GoodsDetail
        fields = [
            'desc',
            'trader_way_name',
            'pub_date',
        ]


class GoodsSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Goods
        fields = [
            'id',
            'name',
            'price',
            'goods_img',
            'category',
            'desc',
            'trader_way_name',
            'pub_date',
            'goods_category_name',
            'goods_create_user',
            'create_user_telephone',
            'create_user_qq',
            'create_user_weixin',
        ]
        extra_kwargs = {
            'id': {'read_only': True},
            'goods_category_name': {'read_only': True},
            "goods_create_user": {'read_only': True},
            'trader_way_name': {'read_only': True},
            'create_user_telephone': {'read_only': True},
            'create_user_qq': {'read_only': True},
            'create_user_weixin': {'read_only': True},
        }

    def create(self, validated_data):
        request = self.context.get('request')
        user = request.user
        validated_data['create_user']= user
        instance = models.Goods.objects.create(**validated_data)
        return instance
